#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTranslator>
#include <QScreen>
#include <QTextStream>

void initQssStyle(QColor color)
{
    QString cssStr;
    if (color == Qt::white)
        cssStr = ":/qss/flatwhite.css";
    else if (color == Qt::black)
        cssStr = ":/qss/psblack.css";
    else if (color == Qt::blue)
        cssStr = ":/qss/projectblue.css";
    else if (color == Qt::lightGray)
        cssStr = ":/qss/lightgray.css";
    else
        cssStr = ":/qss/lightblack.css";

    //加载样式表
    QString qss;
    QFile file(cssStr);

    if (file.open(QFile::ReadOnly))
    {
        //用readAll读取默认支持的是ANSI格式
        //如果不小心用creator打开编辑过了很可能打不开
        QTextStream in(&file);
        in.setCodec("UTF-8"); // 明确指定UTF-8编码
        qss = in.readAll();

        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(QColor(paletteColor)));
        qApp->setStyleSheet(qss);
        file.close();
    }
}
void initLanguage()
{
    QTranslator translator;
    QLocale::Language lab = QLocale::system().language();
    if(QLocale::Chinese == lab)
    {
        translator.load(":/trans/chinese.qm");
        qApp->installTranslator(&translator);
    }else if(QLocale::English== lab){
        translator.load(":/trans/english.qm");
        qApp->installTranslator(&translator);
    }
}

void updateUiScaling() {
    qreal dpi = qApp->primaryScreen()->logicalDotsPerInch();
    qreal scaleFactor = dpi / 96.0; // 基于96DPI基准


    qputenv("QT_SCALE_FACTOR", QByteArray::number(scaleFactor));

    // 设置全局字体
//    QFont font;
//    font.setPointSize(12 * scaleFactor); // 动态调整字号
//    qApp->setFont(font);

}

#include "Capture/RightSetting/objectiveform.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);    // 启用自动高DPI缩放
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);       // 启用高分辨率图标

    QFont font = app.font();
    font.setPixelSize(16);
    app.setFont(font);

    // 初始缩放
    updateUiScaling();

    // 监听DPI变化
//    QObject::connect(app.primaryScreen(), &QScreen::logicalDotsPerInchChanged, &app, [](qreal dpi) {
//        updateUiScaling();
//    });

    initQssStyle(Qt::blue);
//    initLanguage();
    MainWindow w;
    w.show();
//    ObjectiveForm w;
//    w.show();


    return app.exec();
}
